package com.doge.walknovel.business.reader.comments

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.doge.walknovel.api.model.CommentInfo
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.core.base.adapters.toLiveData

class ReaderCommentsViewModel constructor(private val repository: ReaderCommentsRepository) : ViewModel() {
    val loading: LiveData<LoadStatus> = repository.loading
    val dataList = repository.dataSource().toLiveData(
        cellId = { it.id },
        requestMore = { repository.requestChapterComments(false) })

    fun requestChapterComments() = repository.requestChapterComments(true)
    fun requestSubmitChapterComments(content: String?) {
        repository.requestSubmitChapterComments(content)
    }

    fun requestCollectComments(data: CommentInfo?) {
        if (data == null) return
        if (data.likeFlag == 1) repository.requestUnLikeComments(data)
        else repository.requestLikeComments(data)
    }
}


class ReaderCommentsViewModelFactory(private val novelId: Int,
private val chapterId: Int) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val repository = ReaderCommentsRepository(novelId, chapterId)
        if (modelClass.isAssignableFrom(ReaderCommentsViewModel::class.java)) {
            return ReaderCommentsViewModel(repository) as T
        }
        //noinspection unchecked
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

